<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>编辑用户</title>
<script type="text/javascript">
    Ext.onReady(function () {
        var employeeId = <%=params[:id]%>;
        Ext.define('EmployeeEdit', {
            extend: 'Ext.data.Model',
            fields: [
                {
                    name: 'id'
                },
                {
                    name: 'number'
                },
                {
                    name: 'company_id'
                },
                {
                    name: 'login_id'
                },
                {
                    name: 'name'
                },
                {
                    name: 'password'
                },
                {
                    name: 'auth_password'
                },
                {
                    name: 'phone'
                },
                {
                    name: 'identity_card'
                },
                {
                    name: 'address'
                },
                {
                    name: 'email'
                },
                {
                    name: 'department_id', type: 'int'
                },
                {
                    name: 'role_id'
                },
                {
                    name: 'ab'
                },
                {
                    name: 'quit_job', type: 'int'
                },
                {
                    name: 'entry_date',
                    type: 'date',
                    dateFormat: 'Y/m/d'
                },
                {
                    name: 'quit_date',
                    type: 'date',
                    dateFormat: 'Y/m/d'
                }
            ]
        });

        var number = Ext.create('Ext.form.field.Number', {
            fieldLabel: '编号',
            allowBlank: false,
            blankText: "编号不能为空",
            name: 'number',
            anchor: '95%'
        });

        var company = Ext.create('Ext.form.ComboBox', {
            store: companyAllInfoStore,
            fieldLabel: '所属公司',
            allowBlank: false,
            blankText: "所属公司必须选择",
            valueField: 'id',
            displayField: 'name',
            name: 'company_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
            emptyText: '请选择...',
            loadingText: '搜索中...',
            anchor: '95%',
            //pageSize: 10,
            readOnly: false,
            minChars: 0,
            editable: false
        });

        var name = Ext.create('Ext.form.field.Text', {
            fieldLabel: '姓名',
            allowBlank: false,
            blankText: "用户姓名不能为空",
            name: 'name',
            anchor: '95%'
        });

        var authPassword = Ext.create('Ext.form.field.Text', {
            fieldLabel: '授信密码',
            allowBlank: true,
            name: 'auth_password',
            anchor: '95%'
        });

        var password = Ext.create('Ext.form.field.Text', {
            fieldLabel: '登录密码',
            allowBlank: false,
            blankText: "用户名称不能为空",
            name: 'password',
            inputType: 'password',
            anchor: '95%'
        });

        var ab = Ext.create('Ext.form.field.Text', {
            fieldLabel: '姓名缩写',
            allowBlank: false,
            blankText: "姓名缩写",
            name: 'ab',
            anchor: '95%'
        });

        var loginId = Ext.create('Ext.form.field.Text', {
            fieldLabel: '登录Id',
            allowBlank: false,
            blankText: "登录Id不能为空",
            name: 'login_id',
            anchor: '95%'
        });

        var phone = Ext.create('Ext.form.field.Text', {
            fieldLabel: '电话',
            name: 'phone',
            anchor: '95%'
        });

        var identityCard = Ext.create('Ext.form.field.Text', {
            fieldLabel: '身份证',
            name: 'identity_card',
            anchor: '95%'
        });

        var email = Ext.create('Ext.form.field.Text', {
            fieldLabel: '电子邮箱',
            name: 'email',
            anchor: '95%'
        });

        var address = Ext.create('Ext.form.field.Text', {
            fieldLabel: '地址',
            name: 'address',
            anchor: '95%'
        });

        //部门
        var department = Ext.create('Ext.form.ComboBox', {
            store: departmentStore,
            fieldLabel: '公司部门',
            allowBlank: false,
            blankText: "部门必须选择",
            valueField: 'id',
            displayField: 'name',
            name: 'department_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
            emptyText: '请选择...',
            loadingText: '搜索中...',
            anchor: '95%',
            //pageSize: 10,
            readOnly: false,
            minChars: 0,
            editable: false
        });

        //是否离职
        var quitJob = Ext.create('Ext.form.RadioGroup', {
            fieldLabel: '是否离职',
            allowBlank: false,
            //hideLabel:true,
            //style:'margin-left:100px;margin-top:20px',
            columns: 2,
            anchor: '95%',
            vertical: false,
            //name:'quit_job',
            items: [
                {boxLabel: '是', name: 'quit_job', inputValue: '1'},
                {boxLabel: '否', name: 'quit_job', inputValue: '0'}
            ]
        });

        //入职日期
        var entryDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '入职日期',
            name: 'entry_date',
            id: 'entry_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '95%',
            vtype: 'daterange',
            endDateField: 'quit_date'
        });

        var quitDate = Ext.create('Ext.form.field.Date', {
            fieldLabel: '离职日期',
            name: 'quit_date',
            id: 'quit_date',
            format: 'Y-m-d', //格式化日期
            //disabledDays: [0,6],	//不让选择周六，周日
            //disabledDaysText: "周末要休息",
            invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
            anchor: '95%',
            vtype: 'daterange',
            startDateField: 'entry_date'
        });

        var roleStore = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'Role',
            proxy: {
                type: 'ajax',
                url: '/admin/getAllRoles?for_cbb=true',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            },
            //pageSize : pageSize,
            autoLoad: true
        });

        //角色
        var role = Ext.create('Ext.form.ComboBox', {
            store: roleStore,
            fieldLabel: '角色',
            allowBlank: true,
            blankText: "角色必须选择",
            valueField: 'id',
            displayField: 'name',
            name: 'role_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
            emptyText: '请选择...',
            loadingText: '搜索中...',
            anchor: '95%',
            //pageSize: 10,
            readOnly: false,
            minChars: 0,
            editable: false
        });

        var desc = Ext.create('Ext.form.TextArea', {
            fieldLabel: '描述',
            name: 'description',
            height: 80,
            anchor: '95%'
        });

        var editEmployeeForm = Ext.create('Ext.form.Panel', {
            fieldDefaults: {
                labelWidth: 55,
                labelAlign: 'right'
            },
            trackResetOnLoad: true,
            frame: true,
            url: '/employee/update?id=' + employeeId,
            reader: Ext.create('Ext.data.reader.Json', {
                model: 'EmployeeEdit',
                root: 'root'
            }),
            bodyStyle: 'padding:5px 5px 0',
            width: 600,
            //height:300,
            items: [
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .3,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [number]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [company]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .3,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [loginId]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [password]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [authPassword]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .3,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [name]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [phone]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [identityCard]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .3,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [ab]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [department]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [role]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .3,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [quitJob]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [entryDate]
                        },
                        {
                            columnWidth: .35,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [quitDate]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: .5,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [address]
                        },
                        {
                            columnWidth: .5,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [email]
                        }
                    ]
                },
                {
                    layout: 'column',
                    xtype: 'container',
                    items: [
                        {
                            columnWidth: 1,
                            layout: 'anchor',
                            xtype: 'container',
                            items: [desc]
                        }
                    ]
                }
            ],
            buttons: [
                {
                    text: '提交',
                    iconCls: 'icon-save',
                    handler: function () {
                        // check form value
                        if (editEmployeeForm.form.isValid()) {
                            this.disable();
                            editEmployeeForm.form.submit({
                                method: 'GET',
                                success: function (result, response) {
                                    if (response.result.info == "成功") {
                                        //添加成功后，隐藏窗口，并刷新Grid
                                        editEmployeeWin.destroy();
                                    }
                                    else {
                                        Ext.MessageBox.alert('消息', response.result.info);
                                    }
                                },
                                failure: function (result, response) {
                                    Ext.MessageBox.alert('提示', result.responseText);
                                    editEmployeeWin.destroy();
                                }
                            });
                        }
                        else {
                            Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                        }
                    }
                },
                {
                    text: '取消',
                    iconCls: 'exit',
                    handler: function () {
                        editEmployeeWin.destroy();
                    }
                }
            ]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        departmentStore.load({callback: function (record, options, success) {
            if (success) {
                roleStore.load({callback: function (record, options, success) {
                    if (success) {
                        editEmployeeForm.form.load({
                            url: '/employee/edit?id=' + employeeId,
                            method: 'GET',
                            waitMsg: '正在载入数据...',
                            success: function (form, action) {
                            },
                            failure: function (form, action) {
                                Ext.MessageBox.alert('提示信息', '信息加载失败');
                            }
                        });
                    } else {
                        Ext.MessageBox.alert('操作', 'roleStore失败！');
                    }
                }});
            } else {
                Ext.MessageBox.alert('操作', 'departmentStore失败！');
            }
        }});


        editEmployeeForm.render('edit_employee_form');
    })
</script>
</head>
<body>
<div id="edit_employee_form"></div>
</body>
</html>
